package com.ruoyi.web.controller.shop.order;


import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.web.domain.TbOrder;
import com.ruoyi.web.enums.OrderState;
import com.ruoyi.web.service.ITbOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/userOrder")
public class UserOrderController extends BaseController {

    @Value("${ruoyi.orderIdPrefix}")
    String orderIdPrefix;

    @Autowired
    private ITbOrderService tbOrderService;

    /**
     * 提交订单
     */
    @PostMapping("/submitOrder")
    public AjaxResult submitOrder(@RequestBody TbOrder tbOrder) {
        Long userId = getLoginUser().getUserId();
        tbOrder.setOrderId(orderIdPrefix + ((System.currentTimeMillis() + "").split("\\.")[0] + ((int) (Math.random() * 900000 + 100000))));
        tbOrder.setUserId(userId);
        tbOrder.setState(0);
        return toAjax(tbOrderService.insertTbOrder(tbOrder));
    }

    /**
     * 所有订单列表
     *
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo getOrderList() {
        startPage();
        return getDataTable(tbOrderService.getOrderListByUserId(getLoginUser().getUserId()));
    }

    /**
     * 待支付订单列表
     *
     * @return
     */
    @GetMapping("/watPayList")
    public TableDataInfo getWatPayOrderList() {
        startPage();
        return getDataTable(tbOrderService.getOrderListByState(getLoginUser().getUserId(), OrderState.WAIT_PAY.getCode()));
    }

    /**
     * 配送中订单列表
     *
     * @return
     */
    @GetMapping("/deliveryList")
    public TableDataInfo getDeliveryOrderList() {
        startPage();
        return getDataTable(tbOrderService.getOrderListByState(getLoginUser().getUserId(), OrderState.DELIVERY.getCode()));
    }

    /**
     * 待评价订单列表
     *
     * @return
     */
    @GetMapping("/evaluateList")
    public TableDataInfo getEvaluateOrderList() {
        startPage();
        return getDataTable(tbOrderService.getOrderListByUserId(getLoginUser().getUserId()));
    }

    /**
     * 已取消订单列表
     *
     * @return
     */
    @GetMapping("/cancelList")
    public TableDataInfo getCancelOrderList() {
        startPage();
        return getDataTable(tbOrderService.getOrderListByState(getLoginUser().getUserId(), OrderState.ORDER_CANCEL.getCode()));
    }


    // 订单操作

    /**
     * 取消订单
     * @param tbOrder
     * @return
     */
    @DeleteMapping("/cancel")
    public AjaxResult cancelOrder(TbOrder tbOrder) {
        return toAjax(tbOrderService.cancelOrder(tbOrder));
    }
}
